﻿Type.registerNamespace("Al.UI.Controls");

Al.UI.Controls.Repeater = function(element) {
    Al.UI.Controls.Repeater.initializeBase(this, [element]);

    this._expandImageUrl = null;
    this._collapseImageUrl = null;
}

Al.UI.Controls.Repeater.prototype =
{
    initialize: function() {
        Al.UI.Controls.Repeater.callBaseMethod(this, 'initialize');

        this._onClickHandler = Function.createDelegate(this, this._onClick);

        var elements = this._element.getElementsByTagName('img');
        for (var index = 0; index < elements.length; index++) {
            var element = elements[index];
            element.src = this._collapseImageUrl;
            $addHandler(element, 'click', this._onClickHandler);
        }
    },
    dispose: function() {
        var elements = this._element.getElementsByTagName('img');
        for (var index = 0; index < elements.length; index++) {
            var element = elements[index];
            element.src = this._collapseImageUrl;
            $removeHandler(element, 'click', this._onClickHandler);
        }
        Al.UI.Controls.Repeater.callBaseMethod(this, 'dispose');
    },

    get_collapseImageUrl: function() {
        return this._collapseImageUrl;
    },
    set_collapseImageUrl: function(value) {
        if (this._collapseImageUrl !== value) {
            this._collapseImageUrl = value;
            this.raisePropertyChanged('collapseImageUrl');
        }
    },

    get_expandImageUrl: function() {
        return this._expandImageUrl;
    },
    set_expandImageUrl: function(value) {
        if (this._expandImageUrl !== value) {
            this._expandImageUrl = value;
            this.raisePropertyChanged('expandImageUrl');
        }
    },

    _onClick: function(e) {
        var imageId = e.target.id;
        var containerDiv = $get(imageId.substring(0, imageId.length - 4));
        var childDiv = containerDiv.nextSibling;

        if (childDiv.style.display != 'none') {
            childDiv.style.display = 'none';
            e.target.src = this._expandImageUrl;
        }
        else {
            childDiv.style.display = 'block';
            e.target.src = this._collapseImageUrl;
        }
    }
}

// Optional descriptor for JSON serialization.
Al.UI.Controls.Repeater.descriptor = {
    properties: [{ name: 'expandImageUrl', type: String },
                    { name: 'collapseImageUrl', type: String}]
}
 
Al.UI.Controls.Repeater.registerClass('Al.UI.Controls.Repeater', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();